/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName MenuListItemRenderer.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.views.renderers
{
	
	import com.gesturesoft.imultimax.events.TileEvent;
	
	import flash.desktop.Icon;
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	
	import flashx.textLayout.formats.LineBreak;
	
	import mx.core.IDataRenderer;
	import mx.core.mx_internal;
	import mx.graphics.BitmapScaleMode;
	import mx.styles.CSSStyleDeclaration;
	
	import spark.components.IconItemRenderer;
	import spark.components.Image;
	import spark.components.Label;
	import spark.components.VGroup;
	import spark.components.supportClasses.StyleableTextField;
	import spark.utils.MultiDPIBitmapSource;
	

	use namespace mx_internal;

	/**
	 * Styles for MobileTextFields
	 */
	
	public class MenuListItemRenderer extends IconItemRenderer implements IDataRenderer
	{

		//
		// Constants
		//
		public static const IMAGE_WIDTH:int = 75;
		public static const IMAGE_HEIGHT:int = 75;
		public static const LABEL_WIDTH:int = 140;
		public static const LABEL_HEIGHT:int = 36;
		public static const V_PADDING:int = 6;
		public static const H_PADDING:int = 6;
		public static const V_GAP:int = 10;
		public static const H_GAP:int = 10;
		
		//
		// Variables
		//
		private var myFilter:Array = new Array();

		/**
		 * Constuctor
		 */
		public function MenuListItemRenderer()
		{
			
			super();

            labelFunction = getLabelString;
            iconFunction = getIcon;
            
            iconWidth = IMAGE_WIDTH;
            iconHeight = IMAGE_HEIGHT;
            iconScaleMode = BitmapScaleMode.LETTERBOX;
         
			// Add listeners
            addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true);
			
			var dsf:DropShadowFilter = new DropShadowFilter(4,45,0,0.2);
			myFilter.push(dsf);
			
			this.right = 1;
			this.bottom = 1;
			this.cacheAsBitmap = true;
		
		}
		

		/**
		 * Required by IDataRenderer
		 */
		
		override public function set data(value:Object):void
		{
			
			super.data = value;
			
		}

        //--------------------------------------------------------------------------
        //
        //  Methods
        //
        //--------------------------------------------------------------------------
        
        private function getIcon(data:Object):Object
        {
			
            if (data.image)
                return data.image;
            
            return null;
			
        }
        
        private function getLabelString(data:Object):String
        {
			
            if (data.label)
                return data.label;
            
            return null;
			
        }
        
        //
        // Event Handlers
        //
        protected function clickHandler(event:MouseEvent):void
        {
			
			this.alpha = 0.6;
			dispatchEvent(new TileEvent(TileEvent.ITEM_CLICK,data,true));
			
        }
		
        //--------------------------------------------------------------------------
        //
        //  Overridden methods: LabelItemRenderer
        //
        //--------------------------------------------------------------------------
        
        // The message field is used to display the name of the it.  Customize its 
        // appearance by overriding its creation methods
        override protected function createLabelDisplay():void
        {
			
            labelDisplay = StyleableTextField(createInFontContext(StyleableTextField));
            labelDisplay.styleName = this;
			labelDisplay.setStyle('color', styleManager.getStyleDeclaration('.primaryLabel').getStyle('color') );
			labelDisplay.editable = false;
			labelDisplay.selectable = false;
			labelDisplay.cacheAsBitmap = true;
			labelDisplay.multiline = true;
			labelDisplay.wordWrap = true;
			labelDisplay.lineBreak = LineBreak.TO_FIT;
			labelDisplay.height = LABEL_HEIGHT;
			labelDisplay.width = LABEL_WIDTH;
			labelDisplay.setStyle("textAlign","center");
            addChild(labelDisplay);
			
        }
        
        override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void
        {
			
            // Size elements
            
            setElementSize(iconDisplay, IMAGE_WIDTH, IMAGE_HEIGHT);
            setElementSize(labelDisplay, LABEL_WIDTH, LABEL_HEIGHT);
            
            // Calculate the amount of pixels we need to offset to center the components
            var totalHeight:Number = IMAGE_HEIGHT + LABEL_HEIGHT;
            var verticalOffset:Number = (unscaledHeight - totalHeight - V_PADDING) / 2;
            
            // Position the elements
			setElementPosition(iconDisplay, (unscaledWidth/2)-(IMAGE_WIDTH/2), V_PADDING);
            setElementPosition(labelDisplay, H_PADDING, V_PADDING + IMAGE_HEIGHT);
			
            // Invailidate the size so that we can revalidate text due to potential reflow
            if (totalHeight != getElementPreferredHeight(iconDisplay) + getElementPreferredHeight(labelDisplay))
                invalidateSize();
			
        }
        
        
		//--------------------------------------------------------------------------
		//
		//  Overridden methods: UIComponent
		//
		//--------------------------------------------------------------------------

		override protected function measure():void
		{
			
            // Caclulate the initial height of the labelDisplay and iconDisplay
            
            var calculatedHeight:Number = getElementPreferredHeight(iconDisplay) + (V_PADDING * 2) + LABEL_HEIGHT;

			// Round
			calculatedHeight = Math.floor(calculatedHeight);

			// Minimum height of 80 pixels
			calculatedHeight = Math.max(calculatedHeight, 80);

			// Measure the width and Height
			measuredWidth = (H_PADDING * 2) + IMAGE_WIDTH;
			measuredHeight = calculatedHeight;

			// Minimums
			measuredMinWidth = 120;
			measuredMinHeight = calculatedHeight;
			
		}

        override protected function drawBackground(unscaledWidth:Number, unscaledHeight:Number):void
        {
			
			var ss:CSSStyleDeclaration = styleManager.getStyleDeclaration('.listStyle');
			graphics.beginFill(ss.getStyle('gradient1'));
			graphics.drawRect(0,0,unscaledWidth,unscaledHeight);
			graphics.endFill();
            
        }
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			drawBackground(unscaledWidth,unscaledHeight);
			this.filters = myFilter;
			
		}

	}
	
}